/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import client.MailClient;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;
import mail.Message;

/**
 *
 * @author leandro
 */
public class PanelMensaje extends javax.swing.JPanel {
private MailClient cliente;
private DefaultTableModel modelo;
private ArrayList<Message> mensajes;
private int index=-1;

    /**
    /**
     * Creates new form PanelMensaje
     */
    public PanelMensaje(MailClient client) {
        initComponents();     
         this.cliente= client;
        modelo= new DefaultTableModel();
        modelo.addColumn("sender");
        modelo.addColumn("destino");
        modelo.addColumn("asunto");
        TableMensajes.setModel(modelo);
        int cantidad= cliente.getMessagesNuevos().size();
        JOptionPane.showMessageDialog(this, "Tiene "+ cantidad + " mensajes nuevos");
        llenarlabel();
        Timer timer = new Timer( 60000, new ActionListener() { 
            public void actionPerformed(ActionEvent ev) { 
                
               automatico();
            }  
            });
        timer.start();
      }
    private void llenarlabel(){
    int nuevos=cliente.getMessagesNuevos().size();
    int leidos= cliente.getMessagesLeidos().size();
    int borradores= cliente.getMessagesEliminados().size();
    labelnuevos.setText(String.valueOf(nuevos));
    labelleidos.setText(String.valueOf(leidos));
    labelborradores.setText(String.valueOf(borradores));
    }
    public void automatico(){
     cliente.refresh();
        int cantidad= cliente.getMessagesNuevos().size();
        JOptionPane.showMessageDialog(this, "Usted tiene " +cantidad+ " mensajes nuevo/s" );
        llenarlabel();                                     
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        txteliminar = new javax.swing.JButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        TableMensajes = new javax.swing.JTable();
        jRadioButton2 = new javax.swing.JRadioButton();
        radionuevo = new javax.swing.JRadioButton();
        txtarea = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        txtmarcar = new javax.swing.JButton();
        txtrefresh = new javax.swing.JButton();
        labelnuevos = new javax.swing.JLabel();
        labelleidos = new javax.swing.JLabel();
        labelborradores = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(437, 404));

        txteliminar.setText("Eliminar Mensaje");
        txteliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txteliminarActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton3);
        jRadioButton3.setText("Borradores");
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });

        TableMensajes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        TableMensajes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TableMensajesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(TableMensajes);

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("Leidos");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        buttonGroup1.add(radionuevo);
        radionuevo.setText("Nuevos");
        radionuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radionuevoActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        txtarea.setViewportView(jTextArea1);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel3.setText("BANDEJA DE ENTRADA");

        txtmarcar.setText("Marcar");
        txtmarcar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtmarcarActionPerformed(evt);
            }
        });

        txtrefresh.setText("Refresh");
        txtrefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtrefreshActionPerformed(evt);
            }
        });

        labelnuevos.setText("0");

        labelleidos.setText("0");

        labelborradores.setText("0");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtarea, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(txteliminar)
                                .addGap(18, 18, 18)
                                .addComponent(txtmarcar)
                                .addGap(18, 18, 18)
                                .addComponent(txtrefresh))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(17, 17, 17)
                                .addComponent(labelnuevos)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(radionuevo)
                                .addGap(24, 24, 24)
                                .addComponent(labelleidos)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jRadioButton2)
                                .addGap(18, 18, 18)
                                .addComponent(labelborradores)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jRadioButton3))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(116, 116, 116)
                                .addComponent(jLabel3)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(radionuevo)
                    .addComponent(jRadioButton2)
                    .addComponent(jRadioButton3)
                    .addComponent(labelnuevos)
                    .addComponent(labelleidos)
                    .addComponent(labelborradores))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                    .addComponent(txtarea))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txteliminar)
                    .addComponent(txtmarcar)
                    .addComponent(txtrefresh))
                .addContainerGap(64, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txteliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txteliminarActionPerformed
        // TODO add your handling code here:
        if(index!=-1 && !jRadioButton3.isSelected()){
            Message message = mensajes.get(index);
            message.mensajeeliminado();
            mensajes.remove(message);
            cliente.notificarcambios();
            modelo.removeRow(index);
            index=-1;
            llenarlabel();
            jTextArea1.setText("");
        }
    }//GEN-LAST:event_txteliminarActionPerformed

    private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton3ActionPerformed
        // TODO add your handling code here:
        limpiar();
        mensajes = null;
        mensajes = cliente.getMessagesEliminados();
        for (int i = 0; i < mensajes.size(); i++) {
            Message message = mensajes.get(i);
            String cuerpo = message.mostrarMensaje();
            String split[] = cuerpo.split(",");
            Object[] fila = new Object[3];
            fila[0] = split[0];
            fila[1] = split[1];
            fila[2] = split[2];
            modelo.addRow(fila);
        }

    }//GEN-LAST:event_jRadioButton3ActionPerformed
  private void limpiar(){
     modelo= new DefaultTableModel();
      modelo.addColumn("sender");
        modelo.addColumn("destino");
        modelo.addColumn("asunto");
     TableMensajes.setModel(modelo);
     jTextArea1.setText("");
     }
    private void TableMensajesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TableMensajesMouseClicked
        // TODO add your handling code here:
        index = TableMensajes.getSelectedRow();
        Message message = mensajes.get(index);
        String cuerpo = message.mostrarMensaje();
        String texto = cuerpo.split(",")[3];
        jTextArea1.setText(texto);
        //cliente.notificarcambios();
    }//GEN-LAST:event_TableMensajesMouseClicked

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        // TODO add your handling code here:
        limpiar();
        mensajes = null;
        mensajes = cliente.getMessagesLeidos();
        for (int i = 0; i < mensajes.size(); i++) {
            Message message = mensajes.get(i);
            String cuerpo = message.mostrarMensaje();
            String split[] = cuerpo.split(",");
            Object[] fila = new Object[3];
            fila[0] = split[0];
            fila[1] = split[1];
            fila[2] = split[2];
            modelo.addRow(fila);
        }
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void radionuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radionuevoActionPerformed
        // TODO add your handling code here:
        limpiar();
        mensajes = null;
        mensajes = cliente.getMessagesNuevos();
        for (int i = 0; i < mensajes.size(); i++) {
            Message message = mensajes.get(i);
            String cuerpo = message.mostrarMensaje();
            String split[] = cuerpo.split(",");
            Object[] fila = new Object[3];
            fila[0] = split[0];
            fila[1] = split[1];
            fila[2] = split[2];
            modelo.addRow(fila);
        }
    }//GEN-LAST:event_radionuevoActionPerformed

    private void txtmarcarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtmarcarActionPerformed
        // TODO add your handling code here:
        if (index!=-1){
            Message message = mensajes.get(index);
            message.mensajeleido();
            mensajes.remove(message);
            cliente.notificarcambios();
            modelo.removeRow(index);
            index=-1;
            llenarlabel();
            jTextArea1.setText("");
        }
    }//GEN-LAST:event_txtmarcarActionPerformed

    private void txtrefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtrefreshActionPerformed
        // TODO add your handling code here:
        cliente.refresh();
        int cantidad= cliente.getMessagesNuevos().size();
        JOptionPane.showMessageDialog(this, "Usted tiene " +cantidad+ " mensajes nuevo/s" );
        llenarlabel();
        jTextArea1.setText("");
    }//GEN-LAST:event_txtrefreshActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable TableMensajes;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel labelborradores;
    private javax.swing.JLabel labelleidos;
    private javax.swing.JLabel labelnuevos;
    private javax.swing.JRadioButton radionuevo;
    private javax.swing.JScrollPane txtarea;
    private javax.swing.JButton txteliminar;
    private javax.swing.JButton txtmarcar;
    private javax.swing.JButton txtrefresh;
    // End of variables declaration//GEN-END:variables
}
